package com.lovestudying.controller;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.lovestudying.entity.Account;
import com.lovestudying.service.AccountService;
import com.lovestudying.service.impl.AccountServiceImpl;
import com.lovestudying.util.DBUtil_Pro;


/**
 * Servlet implementation class AccountLoginAndLogout
 */
@WebServlet("/AccountLoginAndLogout")
@MultipartConfig

public class AccountLoginAndLogout extends HttpServlet {
	private static final long serialVersionUID = 1L;
      AccountService  accountService = new AccountServiceImpl(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AccountLoginAndLogout() {
        super();
       
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 获取op传入的值
				String op = request.getParameter("op");
				
				// 进行判断
				if(op == null) {
					
					response.sendRedirect("front/login.jsp");
				}else if("login".equals(op)) {
					// 调入登录函数
					loginServlet(request,response);
				}else if("exit".equals(op)) {
					// 获取session对象
					HttpSession session = request.getSession();
					// 将对象存入session作用域中
					session.removeAttribute("phone");
					session.removeAttribute("password");
					session.removeAttribute("name");
					session.removeAttribute("accountPic");
					session.removeAttribute("accountId");
					// 进行重定向
					response.sendRedirect("front/index2.jsp");
				}
			}

	

	private void loginServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// 获取前端取到的值
				String phone = request.getParameter("phone");
				String password = request.getParameter("password");
				String remmber = request.getParameter("checkbox");
				
				String md5Password = DBUtil_Pro.getMd5(password);
				boolean accountLogin = accountService.AccountLogin(phone, md5Password);
				Account account = accountService.getAccountByPhone(phone);
				String name = account.getAccount_name();
				String accountPic = account.getAccount_pic();
				String accountId=account.getAccount_id();
				if(accountLogin == true) {
					// 获取session对象
					HttpSession session = request.getSession();
					// 将对象存入session作用域中
					session.setAttribute("phone", phone);
					session.setAttribute("accountId", accountId);
					session.setAttribute("md5Password", md5Password);
					session.setAttribute("name", name);
					session.setAttribute("accountPic", accountPic);
					if("remmber".equals(remmber)) {
						// 将用户信息存储在cookie中
						Cookie cookie = new Cookie(phone, password);
						response.addCookie(cookie);
					}else {
						// 获取现有的cookie
						Cookie[] cookies = request.getCookies();
						Cookie cookie = null;
						// 进行遍历
						for (int i = 0; i < cookies.length; i++) {
							cookie = cookies[i];
							if(cookie.getName().equals(phone)) {
								// 设置cookie有效时间
								cookie.setMaxAge(0);
								
								// 加入到响应头
								response.addCookie(cookie);
							}
						}
					}
					// 正确 进行重定向
					response.sendRedirect("front/index2.jsp");
				}else {
					// 错误 返回登录页面
					response.sendRedirect("front/login.jsp?message=error");
				}
			}
		}

